<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 内容卡片区 -->
    <el-card class="box-card">
      <!-- 添加分类按钮 -->
      <el-button type="primary" @click="Sortdialog">添加分类</el-button>
      <!-- 内容区 -->
      <tree-table
        :data="goodslist.result"
        :columns="columns"
        :show-index="true"
        :expand-type="false"
        index-text="#"
        :selection-type="false"
      >
        <!-- 是否有效列模板 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: #67c23a"
          ></i>
          <i class="el-icon-error" v-else style="color: #f56c6c"></i>
        </template>
        <!-- 排序列模板 -->
        <template slot="sort" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag
            type="success"
            size="mini"
            v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" v-else size="mini">三级</el-tag>
        </template>
        <!-- 操作列模板 -->
        <template slot="operation" slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="editgood(scope.row)"
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="deletegood(scope.row)"
          ></el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="qureyInfo.pagenum"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="qureyInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="goodslist.total"
      >
      </el-pagination>
      <!-- 添加分类对话框 -->
      <el-dialog
        title="添加分类"
        :visible.sync="dialogVisibleclassification"
        width="50%"
        @close='clear'
      >
        <el-form
          ref="addsort"
          :model="addsort"
          label-width="80px"
          :rules="rules"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addsort.cat_name" class="input"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader 
            :options="goodser"
             clearable 
             v-model='sortform.sortfather' 
             :props='options' 
             size='mini'
             change-on-select="true"
             @change='addsortform'
             >
             </el-cascader>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleclassification = false"
            >取 消</el-button
          >
          <el-button type="primary" @click="SortdialogSbumit">确 定</el-button>
        </span>
      </el-dialog>
     <!-- 编辑分类对话 框-->
     <el-dialog
        title="添加分类"
        :visible.sync="editdialogVisible"
        width="50%"
        @close='clear'
      >
       <el-form
        ref="addsort"
          label-width="80px"
          :rules="rules"
          :model="addsort"
        >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addsort.cat_name" class="input"></el-input>
        </el-form-item>
       </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="editdialogVisible = false"
            >取 消</el-button
          >
          <el-button type="primary" @click="editdialogSbumit">确 定</el-button>
        </span>
     </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rules: {
        cat_name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },
      dialogVisibleclassification: false, //分类对话框开关
      columns: [
        // 第一列
        {
          label: "分类名称",
          prop: "cat_name",
          // width: '40px',
        },
        // 第二列
        {
          label: "是否有效",
          prop: "cat_deleted",
          //  模板类型
          type: "template",
          //  模板名称
          template: "isok",
          // width: '40px',
        },
        // 第三列
        {
          label: "排序",
          prop: "cat_level",
          //  模板类型
          type: "template",
          //  模板名称
          template: "sort",
        },
        // 第四列
        {
          label: "操作",
          prop: "cat_id",
          //  模板类型
          type: "template",
          //  模板名称
          template: "operation",
        },
      ],
      // 分类表单数据
      sortform: {
        sortfather:'',
      },
      // 提交分类 的信息
      addsort:{
         cat_name:'',//分类名称,编辑input框值
         cat_pid:0,//父级ID
         cat_level:0,//分类等级

      },
      // 添加分类 级联选择器数据规则
      options:{
         value: 'cat_id',
         label: 'cat_name',
         children:'children'
      },
      id:0,//分类id
      editdialogVisible : false,//编辑框开关
      goodser: [],//添加分类 级联选择器数据
      goodslist: {}, //商品列表
      qureyInfo: {
        type: 3, //类型大小
        pagenum: 1, //当前页
        pagesize: 5, //页面大小
      },
    };
  },
  created() {
    this.getgoods();
  },
  methods: {
    //   获取商品列表函数
    async getgoods() {
      const { data: res } = await this.$http.get("categories", {
        params: this.qureyInfo,
      });
      if (res.meta.status === 200) {
        this.goodslist = res.data;
      }
      console.log(res);
    },
    //添加分类按钮 点击事件函数
    async Sortdialog() {
      this.dialogVisibleclassification = !this.dialogVisibleclassification;
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 },
      });
      if (res.meta.status === 200) {
        this.goodser = res.data;
        console.log(this.goodser);
      } else {
        console.log("error submit!!");
        return false;
      }
    },
    // 添加分类级联选择器 发生改变
    addsortform(){
    if(this.sortform.sortfather.length>0){
      // 父级ID
      this.addsort.cat_pid=this.sortform.sortfather[this.sortform.sortfather.length-1]
      // 分类等级
      this.addsort.cat_level=this.sortform.sortfather.length
    }else{
       // 父级ID
    this.addsort.cat_pid=0
     // 分类等级
    this.addsort.cat_level=0
    }
    console.log(this.addsort);
    },
    // 添加分类提交 确定按钮点击事件函数
   async SortdialogSbumit() {
    const {data:res}=await this.$http.post('categories',this.addsort)
      if(res.meta.status!==201){
        return this.$message.error(res.meta.msg);
      }else{
        this.getgoods();
        this.dialogVisibleclassification = !this.dialogVisibleclassification;
        return  this.$message({
                message: res.meta.msg,
                type: 'success'
                });
      }
    console.log(res);
    },
    //编辑商品框
    editgood(good) {
     this.editdialogVisible=!this.editdialogVisible
     this.addsort.cat_name=good.cat_name
     this.id=good.cat_id
    },
    // 编辑商品提交
   async editdialogSbumit(){
     const {data:res}=await this.$http.put(`categories/${this.id}`,{cat_name:this.addsort.cat_name})
     if(res.meta.status===200){
       this.editdialogVisible=!this.editdialogVisible
       this.getgoods();
       return  this.$message({
                message: res.meta.msg,
                type: 'success'
                });
     }
    },
    //关闭对话框时清空addsort.cat_name
    clear(){
      this.addsort.cat_name=''
      this.sortform.sortfather=''
    },
    //删除商品
    deletegood(good) {
        this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
         const {data:res}=await this.$http.delete(`categories/${good.cat_id}`)
         if(res.meta.status===200){
           this.getgoods();
         }
          this.$message({
            type: 'success',
            message: res.meta.msg
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 当前页面变化函数
    handleCurrentChange(val) {
      this.qureyInfo.pagenum = val;
      this.getgoods();
    },
    // 页面大小变化函数
    handleSizeChange(val) {
      this.qureyInfo.pagesize = val;
      this.getgoods();
    },
  },
};
</script>

<style scpoed>
.input {
  width: 34% !important;
}
.el-dialog__footer {
  text-align: center !important;
}
.el-cascader-panel{
  height: 244px !important;
}
</style>